package com.gewu.bookeeping.Utils;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Build;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.gewu.bookeeping.IModel.IMenyInDaoModel;
import com.gewu.bookeeping.MyDb.bean.ALIBeanIn;
import com.gewu.bookeeping.MyDb.bean.ALiBeanOut;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanIn;
import com.gewu.bookeeping.MyDb.bean.WeChtBeanOut;
import com.gewu.bookeeping.MyDb.dao.ALiDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.ALiDaoOutPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoInPerson;
import com.gewu.bookeeping.MyDb.dao.WeChtDaoOutPerson;

import java.util.Date;
import java.util.Random;


/**
 * 监听服务类
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@SuppressLint("OverrideAbstract")
public class MyNotiListion extends NotificationListenerService {

    private final static String wechart = "com.tencent.mm";
    private final static String alipay = "com.eg.android.AlipayGphone";
    private final static String message = "com.android.mms";
    private static IMenyInDaoModel.ICallBack callBack;
    private static IMenyInDaoModel.ICallBack2 callBack2;

    /**
     * 因为需要在好多地方进行调用 所以使用单利模式进行返回对象
     *
     * @param context
     * @return
     */
    public static synchronized IMenyInDaoModel.ICallBack getInstance(IMenyInDaoModel.ICallBack context) {
        if (callBack == null) {
            callBack = context;
        }
        return callBack;
    }

    public static synchronized IMenyInDaoModel.ICallBack2 getInstance2(IMenyInDaoModel.ICallBack2 context) {
        if (callBack2 == null) {
            callBack2 = context;
        }
        return callBack2;
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String text2 = extras.getString(Notification.EXTRA_TEXT);
        //判断包名是否和微信包名一样
        Log.i("TAG", "onNotificationPosted:红包 " + text2 + "" + notificationTitle);
        if (notificationPkg.equals(wechart)) {
            //收入
            if (text2.contains("转账") || text2.contains("支付收款") || text2.contains("红包")) {
                WeChtDaoInPerson weChtDaoInPerson = new WeChtDaoInPerson(getApplicationContext());
                WeChtBeanIn weChtBeanIn = new WeChtBeanIn();
                weChtBeanIn.setText("收入类型：" + AppCot.getText(text2));
                weChtBeanIn.setTime(AppCot.getDate(new Date()));
                weChtBeanIn.setType("收入抬头：" + notificationTitle);
                weChtBeanIn.setMoney(Float.valueOf(AppCot.getMoney(text2)));
                weChtDaoInPerson.add(weChtBeanIn);
                callBack.CallBack();
            }
        }

        //盘端是否是阿里巴巴的包名
        if (notificationPkg.equals(alipay)) {
            ALiDaoInPerson aLiDaoInPerson = new ALiDaoInPerson(getApplicationContext());
            ALIBeanIn aliBeanIn = new ALIBeanIn();
            aliBeanIn.setText(text2);
            aliBeanIn.setTime(AppCot.getTime());
            if (text2!=null){
                if (text2.contains("成功")){
                    aliBeanIn.setType("转账");
                }else if (text2.contains("红包")){
                    aliBeanIn.setType("红包");
                }else {
                    aliBeanIn.setType("其他");
                    aliBeanIn.setMoney(Float.valueOf(AppCot.getMoney(text2)));
                }
                aLiDaoInPerson.add(aliBeanIn);
                callBack2.CallBack();
            }
        }

        //判断是否包含本机短信的包名
        if (notificationPkg.equals(message)) {
            String text21 = AppCot.getText2(text2);
            //微信支出
            if (text2.contains("微信")) {
                WeChtDaoOutPerson weChtDaoOutPerson = new WeChtDaoOutPerson(getApplicationContext());
                WeChtBeanOut weChtBeanOut = new WeChtBeanOut();
                weChtBeanOut.setText(text21);
                weChtBeanOut.setTime(AppCot.getTime());
                weChtBeanOut.setMoney(Float.valueOf(AppCot.getMoney2(text21)));
                if (text2.contains("红包")) {
                    //红包支出
                    weChtBeanOut.setType("红包支出");
                } else {
                    //转账支出
                    weChtBeanOut.setType("转账支出");
                }
                weChtDaoOutPerson.add(weChtBeanOut);
                callBack.CallBack();

            } else if (text2.contains("快捷")||text2.contains("POS")) {
//                支付宝的任何支付方式都是快捷支付
                ALiDaoOutPerson aLiDaoOutPerson = new ALiDaoOutPerson(getApplicationContext());
                ALiBeanOut aLiBeanOut = new ALiBeanOut();
                aLiBeanOut.setText(text21);
                aLiBeanOut.setTime(AppCot.getTime());
                aLiBeanOut.setMoney(Float.valueOf(AppCot.getMoney2(text21)));
                if (text2.contains("POS")){
                    aLiBeanOut.setType("收入类型:POS");
                }else {
                    aLiBeanOut.setType("收入类型:快捷支付");
                }
                aLiDaoOutPerson.add(aLiBeanOut);
                callBack2.CallBack();
            }

        }


    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }


}
